/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.migrate;

import com.wtkj.vo.dzzf.base.kfwd.CompanyInfo;
import com.wtkj.vo.dzzf.base.kfwd.UserInfo;
import com.wttech.dzzf.process.dao.ImportHisDataDao;
import com.wttech.dzzf.process.model.CompanyIdKey;
import com.wttech.dzzf.process.model.UserIdKey;
import com.wttech.dzzf.process.service.base.BaseInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * @author MaShibo
 * @date 2017/12/13.
 */
@Service
public class MergeUserInfoService {

	private static final Logger LOGGER = LoggerFactory.getLogger(MergeUserInfoService.class);

	@Resource
	private ImportHisDataDao importHisDataDao;

	@Resource
	private ThreadPoolTaskExecutor executor;

	@Resource
	private BaseInfoService baseInfoService;

	private void processUserInfo() {
		List<UserIdKey> userIdKeys = importHisDataDao.queryAllUserIdKey();
		int size = userIdKeys.size();
		LOGGER.info("共{}个需要合并的证件信息", size);
		List<Future<Void>> futures = new ArrayList<>(size);
		userIdKeys.forEach(userIdKey -> futures.add(executor.submit(() -> {
			processUserIdKey(userIdKey);
			return null;
		})));
		baseInfoService.getFutureResult(futures);
	}

	private void processUserIdKey(UserIdKey userIdKey) {
		Integer userIdType = userIdKey.getUserIdType();
		String userIdNum = userIdKey.getUserIdNum();
		LOGGER.info("正在处理证件号【{}】证件类型【{}】", userIdNum, userIdType);
		List<UserInfo> userInfos = importHisDataDao.queryUserInfosByCert(userIdType, userIdNum);
		if (userInfos.isEmpty()){
			return;
		}
		UserInfo first = userInfos.get(0);
		String userId = first.getId();
		for (int i = 1; i < userInfos.size(); i++) {
			UserInfo userInfo = userInfos.get(i);
			importHisDataDao.updateCardInfoUserId(userId, userInfo.getId());
			importHisDataDao.updateUserInfoOptUserId(userId, userInfo.getId());
			importHisDataDao.deleteRepeatUserInfo(userInfo.getId());
		}
	}

	private void processCompanyInfo() {
		List<CompanyIdKey> companyIdKeys = importHisDataDao.queryAllCompanyKeys();
		int size = companyIdKeys.size();
		LOGGER.info("共【{}】个需要合并的单位客户信息", size);
		List<Future<Void>> futures = new ArrayList<>(size);
		companyIdKeys.forEach(companyIdKey -> {
			futures.add(executor.submit(() -> {
				proessCompanyKey(companyIdKey);
				return null;
			}));
		});
		baseInfoService.getFutureResult(futures);
	}

	private void proessCompanyKey(CompanyIdKey companyIdKey) {
		LOGGER.info("正在处理{}", companyIdKey);
		List<CompanyInfo> companyInfos = importHisDataDao.queryCompanyInfosByCert(companyIdKey);
		CompanyInfo first = companyInfos.get(0);
		String userId = first.getId();
		for (int i = 1; i < companyInfos.size(); i++) {
			CompanyInfo companyInfo = companyInfos.get(i);
			importHisDataDao.updateCardInfoUserId(userId, companyInfo.getId());
			importHisDataDao.updateUserInfoOptUserId(userId, companyInfo.getId());
			importHisDataDao.deleteRepeatCompanyInfo(companyInfo.getId());
		}
	}

	private void shutDown() {
		executor.shutdown();
	}

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		MergeUserInfoService service = context.getBean(MergeUserInfoService.class);
		service.processUserInfo();
		service.shutDown();
	}
}
